import java.util.ArrayList;

/*
rPeanut - is a simple simulator of the rPeANUt computer.
Copyright (C) 2012  Joshua Worth

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
A macro will start with the line:
MACRO
and end with the line:
MEND

 */

// Macro storage/resolving class
public class Macro {
	private ArrayList<String> args;
	private ArrayList<String> lines;
	
	public Macro() {
		args = new ArrayList<String>();
		lines = new ArrayList<String>();
	}
	
	public void addArgument(String arg) {
		args.add(arg);
	}
	
	public void addLine(String line) {
		lines.add(line);
	}
	
	// Get a String suitable for the Assemble.parse() function. This resolves
	// the macro arguments too
	public String getText(ArrayList<String> passargs, Lineinfo li) throws ParseException {
		if (passargs.size() != args.size()) {
			throw new ParseException(li, "calling macro with wrong number of arguments");
		}
		String text = "";
		for (int i = 0; i < lines.size(); i++) {
			String line = lines.get(i);
			for (int j = 0; j < line.length(); j++) {
				if (line.charAt(j) == '&') {
					// Replacement 1: Replace those that are more like arguments (have following whitespace)
					String rest = line.substring(j + 1);
					MyScanner lscn = new MyScanner(rest);
					String aname = lscn.untilSpace();
					int ai;
					if ((ai = args.indexOf(aname)) != -1) {
						int al = args.get(ai).length();
						line = line.substring(0, j) + passargs.get(ai) + line.substring(j + 1 + al);
						j -= al;
					} else {
						// Replacement 2: Replace any other matches (ignoring following whitespace)
						for (int h = 0; h < args.size(); h++) {
							String arg = args.get(h);
							if (rest.startsWith(arg)) {
								int al = arg.length();
								line = line.substring(0, j) + passargs.get(h) + line.substring(j + 1 + al);
								j -= al;
								break;
							}
						}
					}
				}
			}
			text += line + "\n";
		}
		return text;
	}
}
